package com.basics.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.basics.api.request.channel.*;
import com.basics.api.response.channel.*;
import com.basics.api.response.organization.ChannelBaseInfoData;
import com.basics.api.response.organization.ChannelInfoData;
import com.basics.api.response.organization.GroupOrganizationMemberListData;
import com.basics.api.response.organization.OrganizationMemberListData;
import com.basics.api.service.ChannelApiService;
import com.basics.channel.entity.CyChannelGroup;
import com.basics.post.entity.CyChannelPost;
import com.basics.channel.entity.CyChannelTopic;
import com.basics.channel.service.*;
import com.basics.framework.web.domain.RestResult;
import com.basics.post.service.CyChannelPostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/channel/")
@Api(tags = "频道相关")
public class ChannelApiController extends BaseApiController{

    @Autowired
    private ChannelApiService channelApiService;
    @Autowired
    private CyChannelTopicService cyChannelTopicService;
    @Autowired
    private CyChannelGroupUserService cyChannelGroupUserService;
    @Autowired
    private CyChannelGroupService cyChannelGroupService;
    @Autowired
    private CyChannelMsgService cyChannelMsgService;

    @GetMapping("selectHomeChannelList")
    @ApiOperation(value = "首页-获取首页频道列表(需要前端根据showTime排序,showTime可能为null)")
    public RestResult<List<ChannelListData>> selectHomeChannelList(){
        return RestResult.success(channelApiService.selectHomeChannelList(getOrganizationId(),getCurrentUser().getUserId()));
    }

    @GetMapping("getHomeMsgNum")
    @ApiOperation(value = "首页-全部未读消息数量(角标)")
    public RestResult<HomeMsgData> getHomeMsgNum(){
        return RestResult.success(new HomeMsgData(cyChannelMsgService.getUnReadMsgNumByOrganizationId(getOrganizationId(true),getCurrentUser().getUserId())));
    }

    @GetMapping("getTopMsgInfo")
    @ApiOperation(value = "频道详情-获取频道顶部消息推送")
    public RestResult<TopMsgData> getTopMsgInfo(@RequestParam(value = "channelId")Long channelId){
        return RestResult.success(channelApiService.getTopMsgInfo(getCurrentUser().getUserId(),channelId));
    }

    @GetMapping("/selectTopicList")
    @ApiOperation("话题板块-获取频道下的话题板块列表(默认的不可删除)")
    public RestResult<List<CyChannelTopic>> selectTopicList(@RequestParam(value = "channelId")Long channelId){
        return RestResult.success(cyChannelTopicService.list(new QueryWrapper<CyChannelTopic>().eq("channel_id",channelId)
                .orderByDesc("default_flag").orderByAsc("sort_by").eq("del_flag",0)));
    }


    @GetMapping("/getTopicInfo")
    @ApiOperation("添加或删除板块-获取板块详情(用于编辑)")
    public RestResult<CyChannelTopic> getTopicInfo(@RequestParam(value = "topicId")String topicId){
        CyChannelTopic cyChannelTopic = cyChannelTopicService.getById(topicId);
        cyChannelTopic.setGroupNames(cyChannelGroupService.selectByIds(cyChannelTopic.getGroupIds()));
        return RestResult.success(cyChannelTopic);
    }


    @GetMapping("selectChannelMsgList")
    @ApiOperation(value = "点击通知或者右上角邮件图标进入-获取频道消息列表")
    public RestResult<IPage<ChannelMsgListData>> selectChannelMsgList(ChannelMshReq req){
        return RestResult.success(channelApiService.selectChannelMsgList(req,getCurrentUser().getUserId(),getOrganizationId(true)));
    }

    @GetMapping("checkHasPermissionViewChannel")
    @ApiOperation(value = "频道详情-检查是否有权限查看频道(右上角三个点)")
    public RestResult<CheckHasPermissionViewChannelData> checkHasPermissionViewChannel(@RequestParam(value = "channelId")Long channelId){
        return RestResult.success(channelApiService.checkHasPermissionViewChannel(channelId,getCurrentUser().getUserId()));
    }

    @GetMapping("getChannelInfo")
    @ApiOperation(value = "右边更多-获取频道信息")
    public RestResult<ChannelInfoData> getChannelInfo(@RequestParam(value = "channelId")Long channelId){
        return RestResult.success(channelApiService.getChannelInfo(channelId,getCurrentUser().getUserId()));
    }

    @GetMapping("getChannelBaseInfo")
    @ApiOperation(value = "频道名称-获取频道基本信息")
    public RestResult<ChannelBaseInfoData> getChannelBaseInfo(@RequestParam(value = "channelId")Long channelId){
        return RestResult.success(channelApiService.getChannelBaseInfo(channelId));
    }

    @PostMapping("setChannelInfo")
    @ApiOperation(value = "右边更多-设置频道参数(置顶/或者免打扰)")
    public RestResult<String> setChannelInfo(@RequestBody SetChannelReq req){
        channelApiService.setChannelInfo(req,getCurrentUser().getUserId(),getOrganizationId());
        return RestResult.success();
    }

    @PostMapping("setChannelPermission")
    @ApiOperation(value = "权限管理-设置频道权限")
    public RestResult<String> setChannelPermission(@RequestBody SetChannelPermissionReq req){
        channelApiService.setChannelPermission(req,getCurrentUser().getUserId());
        return RestResult.success();
    }

    @GetMapping("getChannelPermission")
    @ApiOperation(value = "权限管理-获取频道权限用于修改")
    public RestResult<ChannelPermissionData> getChannelPermission(@RequestParam(value = "channelId")Long channelId){
        return RestResult.success(channelApiService.getChannelPermission(channelId,getCurrentUser().getUserId()));
    }

    @GetMapping("selectChannelGroupList")
    @ApiOperation(value = "权限管理-获取频道分组列表(用于设置权限的)")
    public RestResult<List<GroupListData>> selectChannelGroupList(@RequestParam(value = "channelId")Long channelId){
        return RestResult.success(channelApiService.selectChannelGroupList(channelId));
    }


    @GetMapping("getChannelGroupInfo")
    @ApiOperation(value = "频道分组-获取频道分组信息")
    public RestResult<GroupChannelData> getChannelGroupInfo(@RequestParam(value = "channelId")Long channelId){
        return RestResult.success(channelApiService.getChannelGroupInfo(channelId,getCurrentUser().getUserId()));
    }

    @GetMapping("getGroupDetail")
    @ApiOperation(value = "添加/修改分组-获取分组详情")
    public RestResult<ChannelGroupInfoData> getGroupDetail(@RequestParam(value = "groupId")String groupId){
        return RestResult.success(channelApiService.getGroupDetail(groupId,getCurrentUser().getUserId()));
    }

    @PostMapping("saveGroup")
    @ApiOperation(value = "添加/修改分组-保存分组信息")
    public RestResult<String> saveGroup(@RequestBody @Valid SaveGroupReq req){
        channelApiService.saveGroup(req,getCurrentUser().getUserId(),getOrganizationId(true));
        return RestResult.success();
    }

    @GetMapping("selectChannelGroupMemberList")
    @ApiOperation(value = "频道-查询频道下分组成员列表")
    public RestResult<IPage<GroupOrganizationMemberListData>> selectChannelGroupMemberList(GroupChannelMemberReq req){
        return RestResult.success(channelApiService.selectChannelGroupMemberList(req,getCurrentUser().getUserId()));
    }

    @GetMapping("selectMemberListByGroupId")
    @ApiOperation(value = "添加/修改分组-查询分组成员列表")
    public RestResult<IPage<OrganizationMemberListData>> selectMemberListByGroupId(GroupMemberReq req){
        return RestResult.success(cyChannelGroupUserService.selectMemberListByGroupId(new Page(req.getPageNum(),req.getPageSize()),req.getGroupId()));
    }

    @GetMapping("deleteGroup")
    @ApiOperation(value = "添加/修改分组-删除分组")
    public RestResult<String> deleteGroup(@RequestParam("groupId")String groupId){
        channelApiService.deleteGroup(groupId,getCurrentUser().getUserId());
        return RestResult.success();
    }

    @GetMapping("/removeGroupChannel")
    @ApiOperation("添加/修改分组-移除用户分组信息")
    public RestResult<String> removeGroupChannel(OperateGroupChannelReq req){
        channelApiService.removeGroupChannel(req,getCurrentUser().getUserId());
        return RestResult.success();
    }

    @PostMapping("/addGroupChannel")
    @ApiOperation("添加/修改分组-添加用户到分组")
    public RestResult<String> addGroupChannel(@RequestBody OperateGroupChannelReq req){
        channelApiService.addGroupChannel(req,getCurrentUser().getUserId());
        return RestResult.success();
    }

    @GetMapping("deleteTopic")
    @ApiOperation(value = "添加或删除板块-删除话题板块")
    public RestResult<String> deleteTopic(@RequestParam("topicId")String topicId){
        channelApiService.deleteTopic(topicId,getCurrentUser().getUserId());
        return RestResult.success();
    }

    @PostMapping("saveTopic")
    @ApiOperation(value = "添加或删除板块-保存话题板块")
    public RestResult<String> saveTopic(@RequestBody SaveTopicReq req){
        channelApiService.saveTopic(req,getCurrentUser().getUserId());
        return RestResult.success();
    }

    @GetMapping("getChannelRule")
    @ApiOperation(value = "内容管理-获取频道帖子屏蔽规则")
    public RestResult<RuleData> getChannelRule(@RequestParam("channelId")Long channelId){
        return RestResult.success(channelApiService.getChannelRule(channelId));
    }

    @PostMapping("saveChannelRule")
    @ApiOperation(value = "内容管理-保存频道帖子屏蔽规则")
    public RestResult<String> saveChannelRule(@RequestBody RuleData req){
        channelApiService.saveChannelRule(req,getCurrentUser().getUserId());
        return RestResult.success();
    }

}
